package com.yi.wood.web.ctrl;

import com.yi.wood.base.Result;
import com.yi.wood.base.ResultCodeEnum;
import com.yi.wood.entity.Classification;
import com.yi.wood.entity.RoleClassification;
import com.yi.wood.service.exception.BusinessException;
import com.yi.wood.web.ctrl.dto.ClassificationParam;
import com.yi.wood.web.service.ClassificationProService;
import com.yi.wood.web.util.WoodBeanUtils;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author moyi
 * create date 2019/4/5 17:14
 */
@Validated
@RestController
@RequestMapping("/api/class/")
public class ClassificationCtrl extends BaseCtrl {

    @Resource
    private ClassificationProService classificationProService;

    /**
     * 添加分类
     *
     * @param param 分类信息
     * @return 添加分类结果
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addClass(@RequestBody @Valid ClassificationParam param) {
        Classification classification = new Classification();
        BeanUtils.copyProperties(param, classification);
        classificationProService.addData(classification);
        return success();
    }

    /**
     * 删除分类
     * @param id ID
     * @return 删除结果
     */
    @RequestMapping(value = "/del/{id}", method = RequestMethod.DELETE)
    public Object deleteClass(@PathVariable Long id) {
        classificationProService.delData(id);
        return success();
    }

    /**
     * 更新分类信息
     * @param param 分类信息
     * @return 更新结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateClass(@RequestBody @Valid ClassificationParam param){
        Classification classification = classificationProService.findClassById(param.getId());
        if (Objects.isNull(classification)){
            throw new BusinessException(new Result(ResultCodeEnum.NOT_FOUND,null));
        }
        BeanUtils.copyProperties(param, classification, WoodBeanUtils.getNullPropertyNames(param));
        classificationProService.updateData(classification);
        return success();
    }

    /**
     * 获取分类列表
     *
     * @param parentId 上级ID
     * @param name     分类名称
     * @param level    分类等级
     * @return 分类列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Object list(@RequestParam(required = false) Long parentId,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) Integer level,
                       @RequestParam(defaultValue = "1") Integer type) {
        return success(classificationProService.list(parentId, name, level, type).values());
    }

    /**
     * 通过角色ID查询 角色所有的分类
     * @param roleId 角色ID
     * @return 分类列表
     */
    @RequestMapping(value = "/role/{roleId}", method = RequestMethod.GET)
    public Object listRoleClass(@PathVariable Long roleId){

        return success();
    }

    /**
     * 给角色添加分类、给分类添加角色
     * @param roleClassification 分类ID和角色额ID
     * @return 结果
     */
    @RequestMapping(value = "/role/add", method = RequestMethod.POST)
    public Object addRoleClass(@RequestBody RoleClassification roleClassification){
        return success();
    }

    /**
     * 删除角色分类的对应关系
     * @param roleClassification 角色ID和分类ID
     * @return 结果
     */
    @RequestMapping(value = "/role/del", method = RequestMethod.POST)
    public Object delRoleClass(@RequestBody RoleClassification roleClassification){
        return success();
    }
}
